package com.tbb.app.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.tbb.app.common.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CacheServiceImpl implements CacheService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void set(String key, Object value) {
        this.set(key, value, 3, TimeUnit.HOURS);
    }

    @Override
    public void set(String key, Object value, long timeout, TimeUnit timeUnit) {
        String v = JSON.toJSONString(value);
        redisTemplate.opsForValue().set(key, v, timeout, timeUnit);
    }

    @Override
    public <T> T get(String key, Class<T> clazz) {
        return JSON.parseObject(redisTemplate.opsForValue().get(key), clazz);
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}
